from 链表 import Node
from 链表.单链表 import HeadSingleLinkedList
from 栈 import Stack

"""
===== 2022.03.14 =====
1.可以专门添加一个属性值保存length,就不需要每次用length方法遍历链表获取长度。
2.头节点可以用来保存一些公共信息，比如链表长度等。
"""


class LinklistStack(Stack):
    """链式栈"""

    def __init__(self, linked_list: HeadSingleLinkedList):
        super().__init__()
        self._items = linked_list

    @property
    def is_empty(self):
        return self._items.is_empty

    def pop(self):
        if self.is_empty:
            print("空栈无元素")
            return
        self._items.remove(self._items.head.next_node)
        print("删除元素成功")
        print(self.get_stack)

    @property
    def get_stack(self):
        return self._items.get_direction()

    def append(self, node):
        self._items.add(node)
        print("添加元素成功")
        print(self.get_stack)

    def top(self):
        return self._items.head.next_node


if __name__ == "__main__":
    # 链式栈
    linked_list = HeadSingleLinkedList()
    ll_stack = LinklistStack(linked_list)
    ll_stack.append(Node("aaa"))
    ll_stack.append(Node("bbb"))
    ll_stack.append(Node("ccc"))
    ll_stack.pop()
    ll_stack.append(Node("ddd"))
    ll_stack.pop()
    ll_stack.pop()
    ll_stack.pop()
